package com.amor.ui.fragment

import android.os.SystemClock
import com.amor.app.BaseFragment
import com.amor.demo.web1.R
import com.amor.demo.web1.databinding.FLaunchBinding
import com.amor.vm.BaseViewModel
import com.amor.ui.MainActivity

/**
 * title 启动页
 * author HeYan
 * date 2021/9/26
 * desc
 */
class LaunchFragment : BaseFragment<FLaunchBinding, BaseViewModel>() {

    private var time = 0L

    override val layoutId: Int = R.layout.f_launch

    override fun initView() {

    }

    override fun initData() {
        time = SystemClock.uptimeMillis()
        delayJump()
    }

    /**
     * 延迟启动
     */
    private fun delayJump() {
        if (activity !is MainActivity)
            return
        val run = Runnable {
            (activity as MainActivity).enterMain()
        }
        val split = SystemClock.uptimeMillis() - time
        if (split < LAUNCH_DELAY) {//不够3秒，延迟启动
            mBinding?.root?.postDelayed(run, LAUNCH_DELAY - split)
        } else {
            run.run()
        }
    }

    companion object {
        private const val LAUNCH_DELAY = 1500L
    }
}